import { makeAutoObservable } from 'mobx';
import { KnowledgeItem, KnowledgeItemFormValues } from './vectorTypes';
import { getKnowledgeItemList, createKnowledgeItem, updateKnowledgeItem, getInfo, deleteKnowledgeItem } from '../../../../apis/backhub/knowledgeItem';
import { message } from 'antd';

class KnowledgeItemStore {
  baseId: string;
  collectionId: string;
  knowledgeItems: KnowledgeItem[] = [];
  modalVisible = false; // 弹窗显示状态
  editingKnowledgeItem: KnowledgeItem | undefined = undefined; // 当前编辑的向量知识库，undefined表示新建
  loading = false;
  total: number = 0;
  currentPage: number = 1;
  pageSize: number = 10;

  constructor() {
    makeAutoObservable(this);
  }

  init = (id) => {
    this.baseId = id;
  };

  // 获取向量知识库列表
  fetchKnowledgeItems = async (collectionId: string, params?: {
    keyword?: string;
    isEmbedded?: number;
    pageNo?: number;
    pageSize?: number;
  }) => {
    this.loading = true;
    try {
      this.collectionId = collectionId;
      const queryParams = {
        collectionId,
        ...params
      };
      const response = await getKnowledgeItemList(queryParams);
      
      if (response.status && response.data) {
        this.knowledgeItems = response.data.records;
        this.total = response.data.total;
        this.currentPage = response.data.current;
        this.pageSize = response.data.size;
      } else {
        this.knowledgeItems = [];
        this.total = 0;
        this.currentPage = 1;
        this.pageSize = 10;
      }
      
      return this.knowledgeItems; 
    } catch (error) {
      console.error('获取向量知识库列表失败:', error);
      this.knowledgeItems = [];
      this.total = 0;
      this.currentPage = 1;
      this.pageSize = 10;
    } finally {
      this.loading = false;
    }
  };

  // 显示创建弹窗
  showCreateModal = () => {
    this.editingKnowledgeItem = undefined;
    this.modalVisible = true;
  };

  // 显示编辑弹窗
  showEditModal = (vector: KnowledgeItem) => {
    this.getKnowledgeItem(vector.id);
    this.modalVisible = true;
  };

  // 隐藏弹窗
  hideModal = () => {
    this.modalVisible = false;
    this.editingKnowledgeItem = undefined;
  };

  //获取
  getKnowledgeItem = async (id: string) => {
    try {
      this.loading = true;
      const response = await getInfo(id);
      this.editingKnowledgeItem = (response as any).data;
      return this.editingKnowledgeItem; // 返回获取到的用户数据
    } catch (error) {
      console.error('获取向量知识库条目:', error);
      throw error;
    } finally {
      this.loading = false;
    }
  };

  // 保存向量知识库
  saveKnowledgeItem = async (values: KnowledgeItemFormValues) => {
    try {
      if (this.editingKnowledgeItem && this.editingKnowledgeItem.id) {
        const response = await updateKnowledgeItem(this.editingKnowledgeItem.id, values);
        if((response as any)?.status){
          message.success('保存成功');
          this.fetchKnowledgeItems(values.collectionId);
        }
      } else {
        const response = await createKnowledgeItem(values);
        if((response as any)?.status){
          message.success('创建成功');
          this.fetchKnowledgeItems(values.collectionId);
        }
      }
      this.hideModal();
    } catch (error) {
      console.error('保存向量知识库条目失败:', error);
    }
  };

  // 删除向量知识库条目
  deleteKnowledgeItem = async (id: string) => {
    try {
      const response = await deleteKnowledgeItem(id);
      message.success('删除知识条目成功');
      if((response as any)?.status){
        this.fetchKnowledgeItems(this.collectionId);
      }
    } catch (error) {
      console.error('删除向量知识库条目失败:', error);
    }
  };
}

export const knowledgeItemStore = new KnowledgeItemStore();